package o2sjsc.core.libs.soap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class SoapUtils {
	public static String Invoke(String url, String methodName,
			HashMap<String, String> hashMap) {

		final String mNamespace = "http://tempuri.org/";
		final String mSoapAction = mNamespace + methodName;
		String mResult;

		try {
			SoapObject request = new SoapObject(mNamespace, methodName);

			/**
			 * Adding properties
			 */
			if (hashMap != null) {
				Map<String, String> map = hashMap;
				Set<String> keys = map.keySet();

				for (Iterator<String> i = keys.iterator(); i.hasNext();) {
					String key = (String) i.next();
					String value = (String) map.get(key);

					request.addProperty(key, value);
				}
			}

			// Declare soap envelope
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(request);
			HttpTransportSE httpTransport = new HttpTransportSE(url);

			// Invoke soap and get response
			httpTransport.call(mSoapAction, envelope);
			Object response = envelope.getResponse();
			mResult = response.toString();

		} catch (Exception exception) {
			mResult = exception.getMessage();
		}

		return mResult;
	}
}
